home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / mach / __msgserver.c < prev    next >
C/C++ Source or Header  |  1993-12-06  |  6KB  |  185 lines

  1. /* Copyright (C) 1993 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* Based on CMU's mach_msg_server.c revision 2.4 of 91/05/14, and thus
  20.    under the following copyright.  Rewritten by Roland McGrath (FSF)
  21.    93/12/06 to use stack space instead of malloc, and to handle
  22.    large messages with MACH_RCV_LARGE.  */
  23.  
  24. /* 
  25.  * Mach Operating System
  26.  * Copyright (c) 1991,1990 Carnegie Mellon University
  27.  * All Rights Reserved.
  28.  * 
  29.  * Permission to use, copy, modify and distribute this software and its
  30.  * documentation is hereby granted, provided that both the copyright
  31.  * notice and this permission notice appear in all copies of the
  32.  * software, derivative works or modified versions, and any portions
  33.  * thereof, and that both notices appear in supporting documentation.
  34.  * 
  35.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  36.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  37.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  38.  * 
  39.  * Carnegie Mellon requests users of this software to return to
  40.  * 
  41.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  42.  *  School of Computer Science
  43.  *  Carnegie Mellon University
  44.  *  Pittsburgh PA 15213-3890
  45.  * 
  46.  * any improvements or extensions that they make and grant Carnegie Mellon
  47.  * the rights to redistribute these changes.
  48.  */
  49. /*
  50.  * HISTORY
  51.  * $Log:    mach_msg_server.c,v $
  52.  * Revision 2.4  91/05/14  17:53:22  mrt
  53.  *     Correcting copyright
  54.  * 
  55.  * Revision 2.3  91/02/14  14:17:47  mrt
  56.  *     Added new Mach copyright
  57.  *     [91/02/13  12:44:20  mrt]
  58.  * 
  59.  * Revision 2.2  90/08/06  17:23:58  rpd
  60.  *     Created.
  61.  * 
  62.  */
  63.  
  64.  
  65. #include <mach.h>
  66. #include <mach/mig_errors.h>
  67. #include <stdlib.h>        /* For malloc and free.  */
  68.  
  69. mach_msg_return_t
  70. __mach_msg_server_timeout (boolean_t (*demux) (mach_msg_header_t *request,
  71.                            mach_msg_header_t *reply),
  72.                mach_msg_size_t max_size,
  73.                mach_port_t rcv_name,
  74.                mach_msg_option_t option,
  75.                mach_msg_timeout_t timeout)
  76. {
  77.   register mig_reply_header_t *request, *reply;
  78.   register mach_msg_return_t mr;
  79.  
  80.   if (max_size == 0)
  81.     {
  82.       option |= MACH_RCV_LARGE;
  83.       max_size = __vm_page_size; /* Generic.  Good? XXX */
  84.     }
  85.  
  86.   request = __alloca (max_size);
  87.   reply = __alloca (max_size);
  88.  
  89.   while (1)
  90.     {
  91.     get_request:
  92.       mr = __mach_msg (&request->Head, MACH_RCV_MSG|option,
  93.                0, max_size, rcv_name,
  94.                timeout, MACH_PORT_NULL);
  95.       while (mr == MACH_MSG_SUCCESS)
  96.     {
  97.       /* We have a request message.
  98.          Pass it to DEMUX for processing.  */
  99.  
  100.       (void) (*demux) (&request->Head, &reply->Head);
  101.  
  102.       switch (reply->RetCode)
  103.         {
  104.         case KERN_SUCCESS:
  105.           /* Hunky dory.  */
  106.           break;
  107.  
  108.         case MIG_NO_REPLY:
  109.           /* The server function wanted no reply sent.
  110.          Loop for another request.  */
  111.           goto get_request;
  112.  
  113.         default:
  114.           /* Some error; destroy the request message to release any
  115.          port rights or VM it holds.  Don't destroy the reply port
  116.          right, so we can send an error message.  */
  117.           request->Head.msgh_remote_port = MACH_PORT_NULL;
  118.           __mach_msg_destroy (&request->Head);
  119.           break;
  120.         }
  121.  
  122.       if (reply->Head.msgh_remote_port == MACH_PORT_NULL)
  123.         {
  124.           /* No reply port, so destroy the reply.  */
  125.           if (reply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)
  126.         __mach_msg_destroy (&reply->Head);
  127.           goto get_request;
  128.         }
  129.  
  130.       /* Send the reply and the get next request.  */
  131.  
  132.       {
  133.         /* Swap the request and reply buffers.  mach_msg will read the
  134.            reply message from the buffer we pass and write the new
  135.            request message to the same buffer.  */
  136.         void *tmp = request;
  137.         request = reply;
  138.         reply = tmp;
  139.       }
  140.  
  141.       mr = __mach_msg (&request->Head,
  142.                MACH_SEND_MSG|MACH_RCV_MSG|option,
  143.                request->Head.msgh_size, max_size, rcv_name,
  144.                timeout, MACH_PORT_NULL);
  145.     }
  146.  
  147.       /* A message error occurred.  */
  148.  
  149.       switch (mr)
  150.     {
  151.     case MACH_RCV_TOO_LARGE:
  152.       /* The request message is larger than MAX_SIZE, and has not
  153.          been dequued.  The message header has the actual size of
  154.          the message.  We recurse here in hopes that the compiler
  155.          will optimize the tail-call and allocate some more stack
  156.          space instead of way too much.  */
  157.       return __mach_msg_server_timeout (demux, request->Head.msgh_size,
  158.                         rcv_name, option, timeout);
  159.  
  160.     case MACH_SEND_INVALID_DEST:
  161.       /* The reply can't be delivered, so destroy it.  This error
  162.          indicates only that the requestor went away, so we
  163.          continue and get the next request.  */
  164.       __mach_msg_destroy (&request->Head);
  165.       break;
  166.  
  167.     default:
  168.       /* Some other form of lossage; return to caller.  */
  169.       return mr;
  170.     }
  171.     }
  172. }
  173.  
  174.  
  175. mach_msg_return_t
  176. __mach_msg_server (demux, max_size, rcv_name)
  177.      boolean_t (*demux) ();
  178.      mach_msg_size_t max_size;
  179.      mach_port_t rcv_name;
  180. {
  181.   return __mach_msg_server_timeout (demux, max_size, rcv_name,
  182.                     MACH_MSG_OPTION_NONE,
  183.                     MACH_MSG_TIMEOUT_NONE);
  184. }
  185.